#include "stdafx.h"
#include "LineDisplay.h"
#include "ComDefinition.h"

CLineDisplay::CLineDisplay(int nType, int nPort)
    : m_nType(nType),
    m_nPort(nPort),
    m_nError(0),
    m_nParity(2),
    m_hCom(NULL)
{
    m_oWrite.hEvent = NULL;
    m_oRead.hEvent = NULL;
}

BOOL CLineDisplay::Initialize()
{
    m_hCom = PR_OpenCom(m_nPort, m_oWrite, m_oRead, m_nParity, m_nError);

    return NULL == m_hCom ? FALSE: TRUE;
}

BOOL CLineDisplay::SendString(LPCTSTR lpStr, int nLen)
{
    return PR_SendStr2Com(m_hCom, lpStr, nLen, m_oWrite);
}

BOOL CLineDisplay::SendStringEx(char *pszText, int nLen)
{
	return PR_SendStr2Com(m_hCom, pszText, nLen, m_oWrite);
}

BOOL CLineDisplay::Close()
{
    return PR_CloseCom(m_hCom, m_oWrite, m_oRead);
}

//////////////////////////////////////////////////////////////////////////
// VC220
CVC220::CVC220(int nPort)
    : CLineDisplay(VC220, nPort)
{

}

BOOL CVC220::Initialize()
{
    CLineDisplay::Initialize();
    return SendString(_T("\x1f"),1);
}

BOOL CVC220::DisplayText(LPCTSTR pStr, int nLength, int nLine)
{
    if (1 == nLine)
    {
        SendString(_T("\x10\x00"),2);
    }
    else
    {
        SendString(_T("\x10\x14"),2);
    }

    return SendString(pStr, nLength);
}

/////////////////////////////////////////////////////////////////////////////
// VC110
CVC110::CVC110(int nPort)
    : CLineDisplay(VC110, nPort)
{
}

BOOL CVC110::Initialize()
{
    if(CLineDisplay::Initialize())
	{
		_QLOG_DEBUG;
        return SendStringEx("\x1b\x40",2);
	}
    else
        return FALSE;
}

BOOL CVC110::DisplayText(LPCTSTR pStr, int nLength, int nLine)
{
    /*
	SendString(_T("\x1b\x51\x41"), 3);
    
    return SendString(pStr, nLength>10 ? 10:nLength);
	*/
	_QLOG_DEBUG;
	char szText[40];
    memset(szText, '\0', 40);
	//wprintf(szText, _T("\xb\x51\x41%s"), pStr);
	printf(szText, "\x1b\x51\x41%s\x0d", (LPSTR)pStr);
	//memcpy((char*)&szText, pStr, 20);
	return SendStringEx(szText, nLength+4);//>10 ? 13:(nLength+3));
}

BOOL CVC110::DisplayTextEx(char *pszText, int nLen)
{
	_QLOG_DEBUG;

	SendStringEx("\x1b\x51\x41",3);
	SendStringEx(pszText, 10);
	SendStringEx("\x0d",1);

	return TRUE;
}

BOOL CVC110::ClearText()
{
    return SendString(_T("\x0c"),1);
}

BOOL CVC110::SetDescriptor(UINT nFlag)
{
    if(nFlag >= 1 && nFlag <=4)
    {
		_QLOG_DEBUG;
        /*SendString(_T("\x1b\x73"), 2);
        CString strFlag(_T("%d"), nFlag);
        return SendString(strFlag, 1);*/

		char szText[4];
		memset(szText, '\0',4);
		sprintf(szText,"\x1b\x73%c", nFlag);
		return SendStringEx(szText, 3);
		/*memset(szText, '\0', 4*sizeof(TCHAR));
		wprintf(szText, _T("\x1b\x73%c"), nFlag);

		return SendString(szText, 3);*/


    }
    return FALSE;
}

BOOL CVC110::ClearDescriptor()
{
    SendString(_T("\x1b\x73"), 2);
    return SendString(CString(_T("%d"), 0), 1);
}